<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>管理后台-修改用户</title>
    <header th:replace="header::html"/>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <input type="hidden" id="userId" name="id" th:value="${user.id}" />
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>用户名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="username" th:value="${user.username}" lay-verify="required"
                               class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为您唯一的登入名
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="nickname" class="layui-form-label">
                        <span class="x-red">*</span>昵称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="nickname" name="nickname" th:value="${user.nickname}" lay-verify="nickname"
                               class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        至少4个字符
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="telephone" class="layui-form-label">
                        <span class="x-red">*</span>手机号
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="telephone" name="telephone" th:value="${user.telephone}" lay-verify="phone"
                               class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        11位数字
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="email" class="layui-form-label">
                        <span class="x-red">*</span>邮箱
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="email" name="email" th:value="${user.email}" lay-verify="email" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="birthday" class="layui-form-label">
                        <span class="x-red">*</span>生日
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="birthday" name="birthday" th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"
                               lay-verify="date" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="sex" value="1" th:field="${user.sex}" lay-skin="primary" title="男">
                        <input type="radio" name="sex" value="0" th:field="${user.sex}" lay-skin="primary" title="女">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>角色
                    </label>
                    <div class="layui-input-block show-role-container"></div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn" lay-filter="edit" lay-submit="" permission="sys:user:edit">修改</button>
                        <button class="layui-btn layui-btn-primary" onclick="xadmin.close();">取消</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        layui.use(['form', 'layer', 'laydate'], function () {
            var form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate;

            // 日期插件
            laydate.render({
                elem: '#birthday',
                format: 'yyyy-MM-dd'
            });

            // 获取角色信息
            getAllRole(function() {
                // 渲染radio
                layui.form.render('radio');
            });

            // 权限校验
            checkPermissoin();

            // 自定义验证规则
            form.verify({
                nickname: function (value) {
                    if (value.length < 4) {
                        return '昵称至少4个字符';
                    }
                }
            });

            // 监听提交
            form.on('submit(edit)', function (data) {
                var roleRadio = $("[name=roleId]:checked");
                if (roleRadio == undefined || roleRadio.length <= 0) {
                    layer.alert("请给该用户设置角色");
                    return false;
                }
                data.field.roleId = roleRadio.val();
                // 发异步请求，修改用户
                $.ajax({
                    url: '/user/edit',
                    type: 'PUT',
                    data: data.field,
                    dataType: 'json',
                    beforeSend: function() {
                        layer.load();
                    },
                    success: function (res) {
                        if (res.httpCode != 200 || res.retCode != 0) {
                            layer.alert(res.retMsg);
                        } else {
                            layer.alert("修改成功", {icon: 6}, function () {
                                // 关闭当前frame
                                xadmin.close();
                                // 对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        }
                    }
                });
                layer.closeAll();
                return false;
            });
        });

        /***
         * 获取角色信息
         *
         * @param callback
         * @return
         */
        function getAllRole(callback) {
            $.ajax({
                url: '/role/all',
                type: 'GET',
                dataType: 'json',
                success: function (res) {
                    var html = "";
                    if (res.data && res.data.length > 0) {
                        for (var i in res.data) {
                            html += '<input type="radio" name="roleId" value="' + res.data[i].id + '" title="' + res.data[i].name + '" />';
                        }
                        $(".show-role-container").html(html);
                        // 获取当前用户的角色
                        getCurrentUserRole(callback);
                    }
                }
            });
        }

        /**
         * 获取当前用户的角色
         * @param callback
         */
        function getCurrentUserRole(callback) {
            var userId = $("#userId").val();

            $.ajax({
                url: '/role/getRoleUserByUserId',
                type: 'GET',
                data: {userId: userId},
                dataType: 'json',
                success: function (res) {
                    if (res.data) {
                        // 选中当前用户的角色
                        $("[name=roleId]").each(function () {
                            if ($(this).val() == res.data) {
                                $(this).attr("checked", true);
                            }
                        });
                    }

                    // 执行完后进行渲染
                    if (callback) {
                        callback();
                    }
                }
            });
        }
    </script>
</body>
</html>
